<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      element-loading-text="加载中"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="社团名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="社团简介" align="center">
        <template slot-scope="scope">
          {{ scope.row.detail }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="170" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.time" class="el-icon-time" />
          <span> {{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" width="110" align="center">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.status==0" type="warning">待审核</el-tag>
          <el-tag v-show="scope.row.status==1" type="info">通过</el-tag>
          <el-tag v-show="scope.row.status==2" type="danger">未通过</el-tag>
          <el-tag v-show="scope.row.status==3" type="success">正常</el-tag>
          <el-tag v-show="scope.row.status==4">冻结</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核" width="100" align="center">
        <template slot-scope="scope">
          <el-button v-show="scope.row.status==3" size="mini" type="primary" @click="setFreeze(scope.row.id,4)">冻结</el-button>
          <el-button v-show="scope.row.status==4" size="mini" type="success" @click="setFreeze(scope.row.id,3)">解冻</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getTeamList, setFreeze } from '@/api/team'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  components: {

  },
  data() {
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      total: 9,
      pageSize: 10
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取表格数据
    fetchData() {
      // debugger
      // console.log('路由模式', process.env.port)
      this.listLoading = true
      const val = {
        page: (this.currentPage - 1), size: this.pageSize
      }
      getTeamList(val).then((res) => {
        console.log(res.res.content)
        this.list = res.res.content
        this.total = res.res.totalElements
        this.currentPage = res.res.number + 1
        this.pageSize = res.res.size
        this.listLoading = false
        console.log(this.list)
      })
    },
    // 审核入驻
    setFreeze(id, status) {
      const val = {
        status: status
      }
      setFreeze(id, val)
        .then((res) => {
          console.log(res)
          this.$message.success('操作成功')
          this.fetchData()
        })
        .catch(() => {
          this.$message.error('操作失败')
        })
    },

    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>
